/* Copyright 2007 Openismus GmbH
 *
 * This program is free software; you can redistribute it and/or modify
 * it under the terms of the GNU General Public License version 2
 * as published by the Free Software Foundation.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program; if not, write to the Free Software
 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
 */

#include <clutter/clutter.h>
#include <stdlib.h>

//ClutterActor *rect = NULL;
//ClutterActor* texture;

gint angles [5] = {-30, -15, 0, 15, 30};

gint rotation_angle = 0;
gint color_change_count = 0;
int ang_id = 0;

void
on_timeline_new_frame (ClutterTimeline *timeline, 
  gint frame_num, gpointer data)
{
    ClutterActor* texture = data;
/*
    if (ang_id < 4)
        ang_id ++;
    else 
        ang_id = 0;
*/
    ang_id = rand () % 5;
  /* Rotate the rectangle clockwise around the z axis, around it's top-left corner: */
  clutter_actor_set_rotation (texture, CLUTTER_Z_AXIS, angles [ang_id],//rotation_angle, 
                                clutter_actor_get_width (texture) / 2, 
                                clutter_actor_get_height (texture) / 2, 
                                0);
}


int main(int argc, char *argv[])
{
    GError* error;

  ClutterColor stage_color = { 0x00, 0x00, 0x00, 0xff };

  clutter_init (&argc, &argv);

  /* Get the stage and set its size and color: */
  ClutterActor *stage = clutter_stage_get_default ();
  clutter_actor_set_size (stage, 240, 320);
  clutter_stage_set_color (CLUTTER_STAGE (stage), &stage_color);
  //clutter_actor_set_scale (stage, 10, 10);

  /* Add a rectangle to the stage: */
/*
  rect = clutter_rectangle_new_with_color (&rect_color);
  clutter_actor_set_size (rect, 70, 70);
  clutter_actor_set_position (rect, 50, 100);
  clutter_container_add_actor (CLUTTER_CONTAINER (stage), rect);
  clutter_actor_show (rect);
*/

    ClutterActor* texture;

    texture = clutter_texture_new_from_file ("redhand.png", NULL);
    if (!texture)
        g_error ("pixbuf load failed!");
/*
    x = CLUTTER_STAGE_WIDTH () / 2;
    y = CLUTTER_STAGE_HEIGHT () / 2;
    clutter_actor_set_position (texture, x, y);
*/
    clutter_actor_set_scale (texture, 0.2, 0.2);
    clutter_container_add (CLUTTER_CONTAINER (stage), texture, NULL);
    clutter_actor_show (texture);

  /* Show the stage: */
  clutter_actor_show (stage);
  
    srand (time (0));

  ClutterTimeline *timeline = clutter_timeline_new(360 /* frames */, 5 /* frames per second. */);
  g_signal_connect (timeline, "new-frame", G_CALLBACK (on_timeline_new_frame),
                        texture);
  clutter_timeline_set_loop(timeline, TRUE); 
  clutter_timeline_start(timeline);

  /* Start the main loop, so we can respond to events: */
  clutter_main ();

  g_object_unref (timeline);

  return EXIT_SUCCESS;

}
